package com.suxin.qrcode;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;

import javax.imageio.ImageIO;
import javax.imageio.stream.FileImageInputStream;
import javax.imageio.stream.ImageInputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Hashtable;

/**
 * @author Tang
 * @classname PictureMainTest
 * @description [  ]
 * @date 2022/8/9 09:19
 */
public class PictureMainTest {

    public static void main(String[] args) throws Exception{
        int width = 500;
        int height = 250;
        String gdNumber = "GD1646709995106";
        String code = "CP20220301005";
        String name = "铝合金";
        String stand = "100mm*100mm";
        int plan = 1000;
        String startDate = "2022-07-08";
        String endDate = "2022-07-08";
        String remark = "这是一个物料！";

        // BufferedImage.TYPE_INT_BGR  jpg
        // BufferedImage.TYPE_INT_ARGB png

        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D graphics = (Graphics2D)bi.getGraphics();

        // 画边框
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0,0,width,height);
        graphics.setColor(Color.black);
        graphics.setStroke(new BasicStroke(2.0f));
        graphics.drawRect(1,1,width - 2,height - 2);

        // 设置标题
        Font title = new Font("宋体", Font.BOLD, 20);
        graphics.setFont(title);
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        FontMetrics titleFontMetrics = graphics.getFontMetrics(title);
        int titleWidth = titleFontMetrics.stringWidth(gdNumber);
        int titleWidthX = (width - titleWidth) / 2;
        graphics.drawString(gdNumber,titleWidthX,30);

        // 画一条横线
        int baseLineNumber = 45;
        graphics.drawLine(0,baseLineNumber,width - 2,baseLineNumber);
        graphics.drawLine(0,baseLineNumber + 20,300,baseLineNumber + 20);
        graphics.drawLine(0,baseLineNumber + 40,300,baseLineNumber + 40);
        graphics.drawLine(0,baseLineNumber + 60,300,baseLineNumber + 60);
        graphics.drawLine(0,baseLineNumber + 80,300,baseLineNumber + 80);
        graphics.drawLine(0,baseLineNumber + 100,300,baseLineNumber + 100);
        graphics.drawLine(0,baseLineNumber + 120,300,baseLineNumber + 120);
        // 物料编号
        graphics.setFont(new Font("宋体", Font.BOLD, 16));
        graphics.drawString("物料编号", 2, 62);
        graphics.setFont(new Font("宋体", Font.PLAIN, 16));
        graphics.drawString(code, 70, 62);
        // 物料名称
        graphics.setFont(new Font("宋体", Font.BOLD, 16));
        graphics.drawString("物料名称", 2, 82);
        graphics.setFont(new Font("宋体", Font.PLAIN, 16));
        graphics.drawString(name, 70, 82);
        // 物料规格
        graphics.setFont(new Font("宋体", Font.BOLD, 16));
        graphics.drawString("物料规格", 2, 102);
        graphics.setFont(new Font("宋体", Font.PLAIN, 16));
        graphics.drawString(stand, 70, 102);
        // 计划数量
        graphics.setFont(new Font("宋体", Font.BOLD, 16));
        graphics.drawString("计划数量", 2, 122);
        graphics.setFont(new Font("宋体", Font.PLAIN, 16));
        graphics.drawString(String.valueOf(plan), 70, 122);
        // 开始时间
        graphics.setFont(new Font("宋体", Font.BOLD, 16));
        graphics.drawString("开始时间", 2, 142);
        graphics.setFont(new Font("宋体", Font.PLAIN, 16));
        graphics.drawString(startDate, 70, 142);
        // 结束时间
        graphics.setFont(new Font("宋体", Font.BOLD, 16));
        graphics.drawString("结束时间", 2, 162);
        graphics.setFont(new Font("宋体", Font.PLAIN, 16));
        graphics.drawString(endDate, 70, 162);
        // 备注
        graphics.setFont(new Font("宋体", Font.BOLD, 16));
        graphics.drawString("备注", 30, 182);
        graphics.setFont(new Font("宋体", Font.PLAIN, 16));
        graphics.drawString(remark, 70, 182);

        // 画竖线
        graphics.drawLine(68,45,68,height - 2);
        graphics.drawLine(300,45,300,height - 2);

        HashMap<EncodeHintType,Object> hints= new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        BitMatrix bitMatrix = new MultiFormatWriter().encode(gdNumber, BarcodeFormat.QR_CODE, 100, 100,hints);
        BufferedImage image1 = MatrixToImageWriter.toBufferedImage(bitMatrix);
        // 二维码
        graphics.drawImage(image1,302,47,190,190,null);
        graphics.dispose();
        ImageIO.write(bi, "PNG", new FileOutputStream("/Users/tanglonglong/Desktop/pic.png"));
    }

}
